package com.xinongbest.cloud.mall.practice.categoryproduct.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinongbest.cloud.mall.practice.categoryproduct.model.dao.CategoryMapper;
import com.xinongbest.cloud.mall.practice.categoryproduct.model.dto.CategoryDto;
import com.xinongbest.cloud.mall.practice.categoryproduct.model.dto.UpdateCategoryDto;
import com.xinongbest.cloud.mall.practice.categoryproduct.model.pojo.Category;
import com.xinongbest.cloud.mall.practice.categoryproduct.model.vo.CategoryVo;
import com.xinongbest.cloud.mall.practice.categoryproduct.service.CategoryService;
import com.xinongbest.cloud.mall.practice.common.exception.ImoocMallException;
import com.xinongbest.cloud.mall.practice.common.exception.ImoocMallExceptionEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void add(CategoryDto categoryDto) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto, category);
        Category oldCategory = categoryMapper.selectByName(category.getName());
        if (oldCategory != null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if (count != 1) {
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }

    @Override
    public void update(UpdateCategoryDto updateCategoryDto) {
        if (updateCategoryDto.getName() != null) {
            Category oldCategory = categoryMapper.selectByName(updateCategoryDto.getName());
            if (oldCategory != null && !updateCategoryDto.getId().equals(oldCategory.getId())) {
                throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
            }
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryDto, category);
            int count = categoryMapper.updateByPrimaryKeySelective(category);
            if (count == 0) {
                throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
            }
        } else {
            throw new ImoocMallException(ImoocMallExceptionEnum.PARAM_NOT_NULL);
        }
    }

    @Override
    public void delete(Integer id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if (category == null) {
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, "type,order_num");
        List<Category> categories = categoryMapper.selectList();
        PageInfo<Category> categoryPageInfo = new PageInfo<>(categories);
        return categoryPageInfo;
    }

    @Override
    @Cacheable(value = "listForConsumer")
    public List<CategoryVo> listForConsumer(Integer categoryId) {
        List<CategoryVo> categoryVos = new ArrayList<>();
        recursivelyFindCategories(categoryVos, categoryId);
        return categoryVos;
    }

    private void recursivelyFindCategories(List<CategoryVo> categoryVoList, Integer parentId) {
        List<Category> categories = categoryMapper.selectListByParentId(parentId);
        if (!CollectionUtils.isEmpty(categories)) {
            CategoryVo categoryVo;
            for (Category category : categories) {
                categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category, categoryVo);
                categoryVoList.add(categoryVo);
                recursivelyFindCategories(categoryVo.getChildCategory(), category.getId());
            }
        }
    }
}
